﻿using System;
using System.IO;
using UnityEngine;

namespace ExceptionModule
{
    public class LogToFile : ICustomLogHandler
    {
        private readonly StreamWriter _streamWriter;

        public LogToFile()
        {
            var filePath = Path.Combine(Application.persistentDataPath, "log.txt");

            var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            _streamWriter = new StreamWriter(fileStream);
        }

        public void HandleLog(string logString, string stackTrace, LogType type)
        {
            _streamWriter.WriteLine($"{DateTime.Now} {type}:{logString}\n{stackTrace}");
            _streamWriter.Flush();
        }

        public void Dispose()
        {
            _streamWriter?.Dispose();
        }
    }
}